$(window).on("load",function(){
	(function($){
		var $nav = $(".trip_nav");
		var navWidth = 1180;
		var navHeight = $nav.height();
		var navTop = $nav.offset().top;
		var target = [];
		var index = 0;
		
		$(".trip_nav li").each(function(index, element) {
			target.push(parseInt($("."+$(this).attr("data-go")).offset().top - 20 - navHeight,10));
			
			$(this).click(function(){
				$("html,body").stop(true).animate({"scrollTop":target[index]});
			});
		});
		
		$(window).scroll(function(){
			var scrolltop = $(window).scrollTop();
			if(scrolltop >= target[0]){
				$nav.css({
					"position":"fixed",
					"z-index":999,
					"width":navWidth,
					"left":"50%",
					"top":0,
					"margin-left":- navWidth / 2
				});
			}else{
				$nav.removeAttr("style");
			}
			for(index = 0; index < target.length; index++){
				if(scrolltop - target[index] < 0){
					break;
				}
			}
			index = index - 1;
			index = index <= 0 ? 0 : index;
			$(".trip_nav li").removeClass("cur").eq(index).addClass("cur");
		});
	})(jQuery);
	(function($){
		var $nav = $(".detail_nav");
		var height = $nav.outerHeight(true);
		var lastHeight = $(".detail_info .item").last().offset().top + $(".detail_info .item").last().outerHeight(true);
		var navHeight = $(".trip_nav").height();
		var navTop = $nav.offset().top;
		var target = [];
		var index = 0;
		
		$(".detail_info .item").each(function(index, element) {
            target.push(parseInt($(this).offset().top - 20 - navHeight,10));
        });
		
		$(".detail_nav li").each(function(index, element) {
			$(this).click(function(){
				$("html,body").stop(true).animate({"scrollTop":target[index]});
			});
		});
		
		$(window).scroll(function(){
			var scrolltop = $(window).scrollTop();
			if(scrolltop >= target[0] && lastHeight - scrolltop - height >= 0){
				$nav.css({
					"top":scrolltop - navTop + 150
				});
			}else if(lastHeight - scrolltop - height < 0){
				$nav.css({
					"top":lastHeight - $(".detail_info").offset().top - height
				});
			}else{
				$nav.removeAttr("style");
			}
			for(index = 0; index < target.length; index++){
				if(scrolltop - target[index] < 0){
					break;
				}
			}
			index = index - 1;
			index = index <= 0 ? 0 : index;
			$(".detail_nav li").removeClass("cur").eq(index).addClass("cur");
		});
	})(jQuery);
	$(window).scroll();
	
	$(".trip_slider").each(function(index, element) {
        var $parent = $(this);
		var $next = $parent.find(".next");
		var $prev = $parent.find(".prev");
		var $wrap = $parent.find("ul");
		var width = $wrap.find("li").first().outerWidth(true);
		var size = $wrap.find("li").size();
		
		$prev.click(function(){
			if(!$wrap.find("li").first().is(":animated")){
				$wrap.find("li").first().clone().appendTo($wrap);
				$wrap.find("li").first().animate({"margin-left":-width},function(){$(this).remove();});
			}
		});
		$next.click(function(){
			if(!$wrap.find("li").first().is(":animated")){
				$wrap.find("li").last().clone().css({"margin-left":-width}).prependTo($wrap);
				$wrap.find("li").first().animate({"margin-left":0},function(){$wrap.find("li").last().remove();});
			}
		});
    });
});
